<?php
	if (empty($_GET['cie'])){
		if (empty($_GET['clasifCIE'])){
			exit;
		}
		else{
			$clasifCIE = strtolower($_GET["clasifCIE"]);
			$clasifCIE = strtoupper(substr($clasifCIE, 0, 1)) . substr($clasifCIE, 1, strlen($clasifCIE) - 1);
			
			include 'config.php';
			$query = 'SELECT c.id, c.codigo AS codigo, c.descripcion, r.rango, r.detalles FROM cie c, rangos_cie r WHERE c.descripcion = "' . $clasifCIE . '" AND c.id_rango_cie_fk = r.id';
			
			try {
				$dbh = new PDO('mysql:host=' . $host . ';dbname=' . $db, $user, $password, $options);
				$json = "";
				foreach($dbh->query($query) as $row) {
					$json .= '{"id": "' . $row['id'] . '", "codigo": "' . htmlspecialchars($row['codigo']) . '", "descripcion": "' . htmlspecialchars($row['descripcion']) . '", "rango": "' . htmlspecialchars($row['rango']) . '", "detalles": "' . htmlspecialchars($row['detalles']) . '"}';
				}
				echo $json;
				$dbh = null;
			} catch (PDOException $e) {
				echo "Error!: " . $e->getMessage() . "<br/>";
				die();
			}
		}
	}
	else{
		$cie = strtoupper($_GET["cie"]);
		
		include 'config.php';
		$query = 'SELECT c.id, c.codigo AS codigo, c.descripcion, r.rango, r.detalles FROM cie c, rangos_cie r WHERE c.codigo = "' . $cie . '" AND c.id_rango_cie_fk = r.id';
		
		try {
			$dbh = new PDO('mysql:host=' . $host . ';dbname=' . $db, $user, $password, $options);
			$json = "";
			foreach($dbh->query($query) as $row) {
				$json .= '{"id": "' . $row['id'] . '", "codigo": "' . htmlspecialchars($row['codigo']) . '", "descripcion": "' . htmlspecialchars($row['descripcion']) . '", "rango": "' . htmlspecialchars($row['rango']) . '", "detalles": "' . htmlspecialchars($row['detalles']) . '"}';
			}
			echo $json;
			$dbh = null;
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
	}
?>